/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VehicleTester.java
 *
 * Created on 29/09/2010, 6:33:09 PM
 */

package Traffic;

import java.util.Random;

/**
 *
 * @author Matthew
 */
public class VehicleTester extends javax.swing.JFrame {

    /** Creates new form VehicleTester */
    public VehicleTester() {
        initComponents();
    }

    VehicleController v;
    Zones z;
    Random r;
    VehicleTesterView vtv;

    public void setup()
    {
        vtv = (VehicleTesterView) jPanel1;
        z = new Zones();
        v = new VehicleController(z);
        r = new Random();
        vtv.setVehicleArray(v.vehicles);

        z.addZone(new Zone(new PositionArea(0,0,50,9), ZoneType.ROAD));
        z.addZone(new Zone(new PositionArea(0,9,50,3), ZoneType.TURN_LANE));
        z.addZone(new Zone(new PositionArea(30,0,1,12), ZoneType.BLOCKAGE));

    }

    private void runCycle()
    {
        v.runCycle();
        Settings.cyclesRun++;


        if (!z.isBlocked(new PositionArea(-3,3,7,3)))
        {
            int vehicleSpeed = (r.nextInt((85/5) - (35/5)) * 5) + 35;
            //System.out.println("Debug: new vehicle speed: " + vehicleSpeed);
            int willTurn = r.nextInt(100);
            VehicleTurn turn = VehicleTurn.NO;


            if (willTurn < 25)
            {
                turn = VehicleTurn.RIGHT;
            }

            v.addVehicle(new Vehicle(new PositionArea(-3,3,3,3),Direction.EAST,vehicleSpeed, turn));
        }
        vtv.repaint();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new VehicleTesterView();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 582, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 342, Short.MAX_VALUE)
        );

        jLabel1.setText("Spawns Vehicles in a single road of 3 lanes with random speeds");

        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 207, Short.MAX_VALUE)
                        .addComponent(jButton1)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        runCycle();
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                VehicleTester vt = new VehicleTester();
                vt.setVisible(true);
                vt.setup();
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables

}
